<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
     // 分类添加页
    public function getAdd(){

        // 查询所有分类
        $cates = self::CateList();
        // dd($cates);
        // dd($cates);
        return view('admin.cate.add',['cates'=>$cates]);
    }

    // 分类添加页
    public function postInsert(Request $request)
    {
        $data = $request->only(['name','pid']);
        // 拼接数据
        // 判断pid是否为0
        if($data['pid'] == 0){
            $data['path'] = '0';
        }else{
            //根据pid查询path
            $res = DB::table('cate')->where('id',$data['pid'])->first();
            $data['path'] = $res->path.','.$data['pid'];
        }
            $data['status'] = 1;
            // dd($data

        // 执行数据操作
        $res = DB::table('cate')->insert($data);
        if($res){
            return redirect('/admin/cate/index')->with('success','分类添加成功');
        }else{
            return back()->with('error','分类添加失败');
        } 
    }

    // 分类列表
    public function getIndex(Request $request)
    {

        $num = $request->input('num',10);
        // $cates = self::CateList();
        if($request->input('keywords')){

         $cates = DB::table('cate')->where('name','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
            $cates=DB::table('cate')->orderBy('path')->paginate($num);
         
        }
        $data=$request->all();

        // var_dump($cates);die;
        // dd($cates);
       foreach($cates as $k => $v) {
        if($v->pid != 0){
            $res = DB::table('cate')->where('id',$v->pid)->first();
            $cates[$k]->path = $res->name;
        }else{
           $cates[$k]->path = '顶级分类'; 
        }
       }

        return view('admin.cate.index',['cates'=>$cates,'request'=>$data]);
    }
   static public function CateList()
    {
         // dd('wwww');
        // $cates = DB::table('cate')->get();
        $cates = DB::select('select *,concat(path,",",id) as paths from cate order by paths');
            // var_dump($cates);die;
        foreach ($cates as $k => $v) {
            // 把字符串变成数组
            $arr = explode(',',$v->path);
            // 获取长度
            $len = count($arr)-1;
            $cates[$k]->name = str_repeat('|----',$len).$v->name;

            # code...
        }
        return $cates;
    }
// 删除分类
     public function postDelete(Request $request)
            {
                // 获取id
                $id = $request->input('id');
                // 检测当前分类下是否还有子分类
                $res = DB::table('cate')->where('pid',$id)->count();
                if($res > 0){
                    // 给ajax返回 不能删除
                    return 1;
                }
                // 执行删除
                $res = DB::table('cate')->where('id',$id)->delete();
                if($res){
                    // 删除成功
                    echo 2;

                }else{
                    // 删除失败
                    echo 0;
                }
            }
    // 分类修改
   public function getEdit($id) 
    {
        $cate = DB::table('cate')->where('id',$id)->first();
        $cates = self::CateList();
        // dd($user);
        // echo '2222';
        return view('admin.cate.edit',['cate'=>$cate,'cates'=>$cates]);
    }

    // 分类数据修改
    public function postUpdate(Request $request)
    {
        // dd($request->all());
        $id = $request->input('id');
        // 查询是否要修改id
        if($request->input('pid') != $request->input('xpid')){
            //要修改分类查询是否由子分类
             $res = DB::table('cate')->where('pid',$id)->count();
             if($res > 0){
                // 有子分类不能修改
                return back()->with('error','有子分类,不能修改当前所属分类');
             }
        }
        // 下面就可以修改
        $data = $request->only(['name','pid']);
        // dd($date);
        // 判断pid是否为0
         if($data['pid'] == 0){
            $data['path'] = '0';
        }else{
            //根据pid查询path
            $res = DB::table('cate')->where('id',$data['pid'])->first();
            $data['path'] = $res->path.','.$data['pid'];
        }
           
            // dd($data

        // 执行数据修改操作
        $res = DB::table('cate')->where('id',$id)->update($data);
        if($res){
            return redirect('/admin/cate/index')->with('success','分类修改成功');
        }else{
            return back()->with('error','分类修改失败');
        } 
    }

    //根据pid获取pid下的所有的子分类
    public static function getCatesByPid($pid)
    {
        $res = DB::table('cate')->where('pid',$pid)->get();
        // dd($res);
        $data = [];
        foreach ($res as $k => $v) {
            $v->sub = self::getCatesByPid($v->id);
            $data[] = $v;
        }
        // dd($data);
        //返回
        return $data;
    }
    //=====================================================
    public static function getAllCate()
    {
        return DB::table('cate')->get();
    }

    //根据pdi查询当前pid下的所有子分类
    public static function getCatesByPidAll($cates,$pid)
    {
        
        $data = [];
        foreach ($cates as $k => $v) {
            if($v->pid == $pid){
                $v->sub = self::getCatesByPidAll($cates,$v->id);
                $data[] = $v;
            }
        }
        //返回
        return $data;
    }

}  
